package cn.shanke.imousdk.api;

import cn.shanke.imousdk.core.SdkClient;
import com.alibaba.fastjson.JSONObject;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class ResApiController {

    @PostMapping("/startLive")
    public JsonResult startLive(@RequestBody JSONObject request) {
        String deviceId = request.getString("deviceId");
        String channelId = request.getString("channelId");
        if (StringUtils.isEmpty(deviceId) || StringUtils.isEmpty(channelId)) {
            return JsonResult.error("设备编号或通道号错误");
        }
        /**
         * const url = 'imou://open.lechange.com/5K0516FPAZ729F0/1/1?streamId=1';
         * const kitToken = 'Kt_cfd831004f824a269f533e970aad0960';
         */
        String kitToken = SdkClient.getKitToken(deviceId, channelId);
        if (StringUtils.isEmpty(kitToken)) {
            return JsonResult.error("获取kitToken失败");
        }
        JSONObject data = new JSONObject();
        data.put("url", String.format("imou://open.lechange.com/%s/%s/1?streamId=1", deviceId, channelId));
        data.put("kitToken", kitToken);
        return JsonResult.success().withData(data);
    }
}
